package cn.com.xyc.datasource.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import cn.com.xyc.datasource.dao.UserDao;
import cn.com.xyc.datasource.pojo.User;
import cn.com.xyc.datasource.pojo.UserMapper;

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

	@Override
	public void save(User user) {
		System.out.println("采用JDBC技术保存用户信息");
		String sql = "insert into d_user (email,nickname,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip) values(?,?,?,?,?,?,?,?)";
		this.getJdbcTemplate().update(
				sql,
				new Object[] { user.getEmail(), user.getNickname(),
						user.getPassword(), user.getUserIntegral(),
						user.isEmailVerify() ? "T" : "N",
						user.getEmailVerifyCode(), user.getLastLoginTime(),
						user.getLastLoginIp() });

	}

	@Override
	public void update(User user) {
		System.out.println("采用JDBC技术更新用户信息");
		String sql = "update d_user set email=?,nickname=?,password=?,user_integral=?,is_email_verify=?,email_verify_code=?,last_login_time=?,last_login_ip=? where id=?";
		this.getJdbcTemplate().update(
				sql,
				new Object[] { user.getEmail(), user.getNickname(),
						user.getPassword(), user.getUserIntegral(),
						user.isEmailVerify() ? "T" : "N",
						user.getEmailVerifyCode(), user.getLastLoginTime(),
						user.getLastLoginIp() });
	}

	@Override
	public void deleteById(int id) {
		String sql = "delete from d_user where id=?";
		this.getJdbcTemplate().update(sql, new Object[] { id });
	}

	@Override
	public User findById(int id) {
		String sql = "select * from d_user where id=?";
		return (User) this.getJdbcTemplate().queryForObject(sql,
				new Object[] { id }, new UserMapper());
	}

	@Override
	public List<User> findAll() {
		String sql = "select * from d_user";
		List list = this.getJdbcTemplate().query(sql, new UserMapper());
		return list;
	}

	@Override
	public int count() {
		String sql = "select count(1) from d_user";
		return this.getJdbcTemplate().queryForInt(sql);
	}

}
